<!-- components/card-design.vue -->
<template>
  <view class="card-design">
    <!-- 主卡片容器 -->
    <view 
      class="card-container"
      :class="{ 'card-loading': loading, 'card-h5': isH5 }"
      :style="cardContainerStyle"
    >
      <!-- 加载状态 -->
      <view v-if="loading" class="card-loading-content">
        <view class="loading-spinner"></view>
        <text class="loading-text">加载会员卡信息...</text>
      </view>
      
      <!-- 卡片内容 -->
      <view v-else class="card-content">
        <!-- 卡片背景 -->
        <view class="card-background" :style="backgroundStyle">
          <!-- 背景装饰元素 -->
          <view class="bg-decoration bg-circle-1"></view>
          <view class="bg-decoration bg-circle-2"></view>
          <view class="bg-decoration bg-wave"></view>
        </view>
        
        <!-- 卡片主体内容 -->
        <view class="card-main">
          <!-- 顶部区域：Logo和卡名称 -->
          <view class="card-header">
            <view class="card-logo">
              <image 
                v-if="cardInfo.logo" 
                :src="cardInfo.logo" 
                class="logo-image"
                mode="aspectFit"
              />
              <text v-else class="logo-text">{{ cardInfo.shortTitle || '会员' }}</text>
            </view>
            <view class="card-title-section">
              <text class="card-title">{{ cardInfo.title || '会员卡' }}</text>
              <text class="card-subtitle">{{ cardInfo.subtitle || '尊享会员特权' }}</text>
            </view>
          </view>
          
          <!-- 中间区域：卡号和等级 -->
          <view class="card-body">
            <view class="card-number-section">
              <text class="number-label">卡号</text>
              <view class="card-number">
                <text 
                  v-for="(num, index) in displayCardNumber" 
                  :key="index"
                  class="number-digit"
                >
                  {{ num }}
                </text>
              </view>
            </view>
            
            <view class="card-level-section">
              <view class="level-badge" :style="levelBadgeStyle">
                <text class="level-text">{{ cardInfo.level || '普通会员' }}</text>
              </view>
              <view class="card-benefits">
                <text class="benefits-text">{{ benefitsText }}</text>
              </view>
            </view>
          </view>
          
          <!-- 底部区域：有效期和特权 -->
          <view class="card-footer">
            <view class="valid-date">
              <text class="valid-label">有效期</text>
              <text class="valid-value">{{ validDateText }}</text>
            </view>
            
            <view class="privileges">
              <view 
                v-for="(privilege, index) in displayPrivileges" 
                :key="index"
                class="privilege-item"
              >
                <text class="privilege-icon">✓</text>
                <text class="privilege-text">{{ privilege }}</text>
              </view>
            </view>
          </view>
        </view>
        
        <!-- 卡片光泽效果 -->
        <view class="card-shine"></view>
      </view>
    </view>
    
    <!-- 卡片说明 -->
    <view v-if="!loading" class="card-description">
      <view class="description-item">
        <text class="description-icon">💎</text>
        <text class="description-text">{{ cardInfo.description || '享受专属会员权益' }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'CardDesign',
  props: {
    cardInfo: {
      type: Object,
      default: () => ({})
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isH5: false,
      defaultCardInfo: {
        title: 'VIP会员卡',
        subtitle: '尊享会员特权',
        level: '黄金会员',
        backgroundColor: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
        textColor: '#ffffff',
        privileges: ['专属折扣', '积分奖励', '生日特权', '优先服务'],
        validDate: '永久有效',
        benefits: '享受购物折扣、积分奖励等专属权益'
      }
    };
  },
  computed: {
    // 合并默认数据和传入数据
    mergedCardInfo() {
      return {
        ...this.defaultCardInfo,
        ...this.cardInfo
      };
    },
    
    // 卡片容器样式
    cardContainerStyle() {
      return {
        background: this.mergedCardInfo.backgroundColor,
        color: this.mergedCardInfo.textColor,
        boxShadow: this.mergedCardInfo.shadow || '0 20rpx 60rpx rgba(0, 0, 0, 0.15)'
      };
    },
    
    // 背景样式
    backgroundStyle() {
      return {
        background: this.mergedCardInfo.backgroundImage ? 
          `url(${this.mergedCardInfo.backgroundImage})` : 
          'transparent',
        opacity: this.mergedCardInfo.backgroundImage ? 0.1 : 0
      };
    },
    
    // 等级徽章样式
    levelBadgeStyle() {
      return {
        background: 'rgba(255, 255, 255, 0.2)',
        border: `1rpx solid ${this.mergedCardInfo.textColor}`
      };
    },
    
    // 显示卡号（格式化）
    displayCardNumber() {
      const number = this.mergedCardInfo.cardNumber || '8888 8888 8888 8888';
      return number.replace(/\s/g, '').split('');
    },
    
    // 权益文本
    benefitsText() {
      return this.mergedCardInfo.benefits || '享受专属会员权益';
    },
    
    // 有效期文本
    validDateText() {
      return this.mergedCardInfo.validDate || '永久有效';
    },
    
    // 显示的特权列表
    displayPrivileges() {
      const privileges = this.mergedCardInfo.privileges || [];
      return privileges.slice(0, 2); // 只显示前两个特权
    }
  },
  mounted() {
    this.isH5 = this.$platform?.isH5 || false;
  },
  methods: {
    // 可以添加一些交互方法，比如点击卡片翻转等
    handleCardClick() {
      this.$emit('card-click', this.mergedCardInfo);
    }
  }
};
</script>

<style scoped>
.card-design {
  padding: 30rpx;
}

/* 卡片容器 */
.card-container {
  border-radius: 30rpx;
  overflow: hidden;
  position: relative;
  min-height: 400rpx;
  transition: all 0.3s ease;
  backdrop-filter: blur(10rpx);
}

.card-container.card-h5 {
  cursor: pointer;
}

.card-container.card-h5:active {
  transform: scale(0.98);
}

.card-container.card-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%) !important;
}

/* 加载状态 */
.card-loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60rpx;
}

.loading-spinner {
  width: 60rpx;
  height: 60rpx;
  border: 4rpx solid rgba(255, 255, 255, 0.3);
  border-top: 4rpx solid #ffffff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 30rpx;
}

.loading-text {
  color: #ffffff;
  font-size: 28rpx;
  opacity: 0.9;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 卡片背景 */
.card-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-size: cover;
  background-position: center;
}

.bg-decoration {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.bg-circle-1 {
  width: 200rpx;
  height: 200rpx;
  top: -100rpx;
  right: -50rpx;
}

.bg-circle-2 {
  width: 150rpx;
  height: 150rpx;
  bottom: -75rpx;
  left: -50rpx;
}

.bg-wave {
  width: 300rpx;
  height: 100rpx;
  bottom: 20rpx;
  right: -100rpx;
  border-radius: 50%;
  opacity: 0.05;
}

/* 卡片主体内容 */
.card-content {
  position: relative;
  z-index: 2;
  padding: 40rpx;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* 卡片头部 */
.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 50rpx;
}

.card-logo {
  width: 100rpx;
  height: 100rpx;
  border-radius: 20rpx;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 30rpx;
  backdrop-filter: blur(10rpx);
  border: 1rpx solid rgba(255, 255, 255, 0.3);
}

.logo-image {
  width: 60rpx;
  height: 60rpx;
  border-radius: 12rpx;
}

.logo-text {
  font-size: 24rpx;
  font-weight: bold;
  color: #ffffff;
}

.card-title-section {
  flex: 1;
}

.card-title {
  display: block;
  font-size: 44rpx;
  font-weight: bold;
  margin-bottom: 8rpx;
  text-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.card-subtitle {
  display: block;
  font-size: 24rpx;
  opacity: 0.9;
  text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

/* 卡片主体 */
.card-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-bottom: 40rpx;
}

.card-number-section {
  margin-bottom: 40rpx;
}

.number-label {
  display: block;
  font-size: 24rpx;
  opacity: 0.8;
  margin-bottom: 20rpx;
}

.card-number {
  display: flex;
  gap: 20rpx;
}

.number-digit {
  font-size: 36rpx;
  font-weight: bold;
  font-family: 'Courier New', monospace;
  text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
}

.card-level-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.level-badge {
  padding: 12rpx 24rpx;
  border-radius: 20rpx;
  backdrop-filter: blur(10rpx);
}

.level-text {
  font-size: 24rpx;
  font-weight: bold;
}

.card-benefits {
  flex: 1;
  text-align: right;
}

.benefits-text {
  font-size: 24rpx;
  opacity: 0.9;
}

/* 卡片底部 */
.card-footer {
  border-top: 1rpx solid rgba(255, 255, 255, 0.2);
  padding-top: 30rpx;
}

.valid-date {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.valid-label {
  font-size: 24rpx;
  opacity: 0.8;
}

.valid-value {
  font-size: 24rpx;
  font-weight: bold;
}

.privileges {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
}

.privilege-item {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.1);
  padding: 12rpx 20rpx;
  border-radius: 20rpx;
  backdrop-filter: blur(10rpx);
}

.privilege-icon {
  font-size: 20rpx;
  margin-right: 12rpx;
  opacity: 0.8;
}

.privilege-text {
  font-size: 22rpx;
  opacity: 0.9;
}

/* 卡片光泽效果 */
.card-shine {
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    45deg,
    transparent 30%,
    rgba(255, 255, 255, 0.1) 50%,
    transparent 70%
  );
  transform: rotate(45deg);
  animation: shine 3s ease-in-out infinite;
  pointer-events: none;
}

@keyframes shine {
  0%, 100% {
    transform: rotate(45deg) translateX(-100%);
  }
  50% {
    transform: rotate(45deg) translateX(100%);
  }
}

/* 卡片说明 */
.card-description {
  margin-top: 30rpx;
  padding: 30rpx;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20rpx;
  backdrop-filter: blur(10rpx);
}

.description-item {
  display: flex;
  align-items: center;
}

.description-icon {
  font-size: 32rpx;
  margin-right: 20rpx;
}

.description-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
}

/* 响应式设计 */
@media (max-width: 375px) {
  .card-design {
    padding: 20rpx;
  }
  
  .card-content {
    padding: 30rpx;
  }
  
  .card-title {
    font-size: 38rpx;
  }
  
  .number-digit {
    font-size: 32rpx;
  }
}

/* 暗色模式支持 */
@media (prefers-color-scheme: dark) {
  .card-description {
    background: rgba(0, 0, 0, 0.1);
  }
  
  .description-text {
    color: rgba(255, 255, 255, 0.8);
  }
}
</style>